﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using TotalCrytography.Protector;
using System.Runtime.Serialization.Formatters.Binary;
using TotalCrytography.SystemFile;
using System.Collections;
using System.Security.Cryptography;
using TotalCrytography.Data;

namespace TotalCrytography
{
    public partial class EncryptGUI : Form
    {
        String _publicKey;
        Symmetric_Provider _sp;
        private String _strOutputFile;
        private List<String> _lstSelectFile;
        PaddingMode _pm;
        CipherMode _cm;
        SymmetricAlgorithm _sa;
        int _typeAl;//algorithm
        int _iPM;
        int _iCM;

        public EncryptGUI(List<String> selectFile)
        {
            InitializeComponent();
            _lstSelectFile = selectFile;
            _sp = new Symmetric_Provider(new DESCryptoServiceProvider(),PaddingMode.ANSIX923,CipherMode.ECB);
            _sa = new DESCryptoServiceProvider();
            _pm = PaddingMode.ANSIX923;
            _cm = CipherMode.ECB;
            _typeAl = 0;
            _iPM = 0;
            _iCM = 0;

            ckZip.Checked = false;
            rbAfter.Enabled = false;
            rbBefore.Enabled = false;
            _publicKey = string.Empty;
        }

        

        private void EncryptGUI_Load(object sender, EventArgs e)
        {
            //path
            string temp = _lstSelectFile[0].Substring(0, _lstSelectFile[0].LastIndexOf('\\')+1);
            tbPath.Text = temp;

            //reciver
            ArrayList lstUser = TotalCrytography.Data.UserBUS.GetListUsers();
            
            cbReceiver.DisplayMember = "Email";
            cbReceiver.ValueMember = "PublicKey";
            cbReceiver.DataSource = lstUser;
            //algorithm
            String[] algorithm = new String[] { "DES", "Triple DES", "RC2", "Rijndael" };
            cbAlgorithm.DataSource = algorithm;

            //padding mode
            String[] paddingmode = new String[] { "X923", "PKCS7", "ISO 10126"};
            cbPadding.DataSource = paddingmode;
            //mode operation
            String[] operation = new String[] { "ECB", "CBC", "OFB", "CFB", "CTS" };
            cbModeOperation.DataSource = operation;
        }

        private void btEncrypt_Click(object sender, EventArgs e)
        {

            //choose algorithm
            try
            {
                if (ckZip.Checked)
                {
                    if (rbBefore.Checked)
                    {
                        for (int i = 0; i < _lstSelectFile.Count; i++)
                        {

                            Zip.Zip.ZipFiles(_lstSelectFile.GetRange(0, 1), _lstSelectFile[i] + ".zip", "", 0);
                            Encrypt(i, ".zip");

                            File.Delete(_lstSelectFile[i] + ".zip");//delete temp file
                        }
                    }
                    else
                    {
                        for (int i = 0; i < _lstSelectFile.Count; i++)
                        {
                            Encrypt(i, "");
                            //String inputPath = _lstSelectFile[0] + ".enc";
                            String inputFile = _lstSelectFile[i] + ".enc";
                            String outputFile = tbPath.Text + '\\' + inputFile.Substring(inputFile.LastIndexOf('\\') + 1);
                            Zip.Zip.ZipFiles(new String[] { outputFile }.ToList(), outputFile + ".zip", "", 0);

                            File.Delete(outputFile);//delete temp file
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < _lstSelectFile.Count; i++)
                    {
                        Encrypt(i, "");
                    }

                }


                MessageBox.Show("Encrypt Successfully!");
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK);
            }
        }

        void Encrypt(int index, String ext)
        {

            String inputFile = _lstSelectFile[index] + ext;
            
            
            if (Directory.Exists(inputFile))//is folder
            {
                String outputFile = tbPath.Text + '\\' + inputFile.Substring(inputFile.LastIndexOf('\\') + 1);
                MyFolder mfl = new MyFolder(inputFile);
                mfl.Encrypt(outputFile, _sp, _typeAl, _iPM, _iCM, _publicKey);
            }
            else//is file
            {
                String outputFile = tbPath.Text + '\\' + inputFile.Substring(inputFile.LastIndexOf('\\') + 1) + ".enc";
                MyFile f = new MyFile(inputFile);
                f.Encrypt(inputFile, outputFile, TypeOfFile.File, _sp, _typeAl, _iPM, _iCM, _publicKey);
            }

        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
                tbPath.Text = fd.SelectedPath;
        }

        private void tbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbReceiver_SelectedIndexChanged(object sender, EventArgs e)
        {
            //_publicKey = ((UserDTO)cbReceiver.SelectedValue).PublicKey;
            _publicKey = cbReceiver.SelectedValue.ToString();
            
        }

        private void cbAlgorithm_SelectedIndexChanged(object sender, EventArgs e)
        {
            _typeAl = cbAlgorithm.SelectedIndex;
        }

        private void cbPadding_SelectedIndexChanged(object sender, EventArgs e)
        {

            _iPM = cbPadding.SelectedIndex;
        }

        private void cbModeOperation_SelectedIndexChanged(object sender, EventArgs e)
        {
            _iCM = cbModeOperation.SelectedIndex;
        }

        private void ckZip_CheckedChanged(object sender, EventArgs e)
        {
            rbAfter.Enabled = ckZip.Checked;
            rbBefore.Enabled = ckZip.Checked;
        }



    }
}
